package com.example.dense_rack_server.controller;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.example.dense_rack_server.entity.HostTemHum;
import com.example.dense_rack_server.service.HostTemHumService;
import com.example.dense_rack_server.utils.ResponseModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

/**
 * <p>
 * 温湿度 前端控制器
 * </p>
 *
 * @author  张彬 
 * @since 2025-03-18
 */
@RestController
@RequestMapping("/host/tem/hum")
public class HostTemHumController {

    @Resource
    private HostTemHumService hostTemHumService;


    /**
     * 获取折线图
     * @return
     * @throws ParseException
     */
    @GetMapping("/line")
    public Map<String, Object> getHostTemHumList(@RequestParam(value = "selectStartDate", required = false) String selectStartDate,
                                                 @RequestParam(value = "selectEndDate", required = false) String selectEndDate,
                                                 @RequestParam(value = "hostId", required = false) String hostId) {
        return hostTemHumService.getHostTemHumList(selectStartDate, selectEndDate, hostId);
    }



    @GetMapping("/insert")
    public ResponseModel getHostTemHumList() {
        for (int i = 0; i < 1000; i++) {
            HostTemHum hostTemHum = new HostTemHum();
            hostTemHum.setTemperature(Convert.toStr(RandomUtil.randomInt(8, 100)));
            hostTemHum.setHumidity(Convert.toStr(RandomUtil.randomInt(10, 100)));
            String randomDate = randomDate();
            System.out.println(randomDate);
            hostTemHum.setCreateTime(randomDate);
            hostTemHum.setHostId(6);
            hostTemHumService.save(hostTemHum);
        }
        return ResponseModel.success(null);
    }

    /**
     * 随机时间
     * @return
     */
    public String randomDate() {
        // 定义开始时间和结束时间
        Date nowDate = DateUtil.date();
        Date startDate = DateUtil.offset(nowDate, DateField.MONTH, -1);

        LocalDateTime start = LocalDateTime.of(DateUtil.year(startDate), DateUtil.month(startDate)+1, DateUtil.dayOfMonth(startDate), 1, 0, 0);
        LocalDateTime end = LocalDateTime.of(DateUtil.year(nowDate), DateUtil.month(nowDate)+1, DateUtil.dayOfMonth(nowDate), 23, 59);

        // 计算时间范围内的随机时间点
        long startEpoch = start.toEpochSecond(ZoneOffset.ofHours(0));
        long endEpoch = end.toEpochSecond(ZoneOffset.ofHours(0));
        long randomEpoch = ThreadLocalRandom.current().nextLong(startEpoch, endEpoch);

        LocalDateTime randomDateTime = LocalDateTime.ofEpochSecond(randomEpoch, 0, ZoneOffset.UTC);

        // 定义时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime = randomDateTime.format(formatter);

        // 输出结果
        System.out.println("随机生成的时间字符串: " + formattedTime);
        return formattedTime;
    }

}
